#include "head.h"

Screen::Screen(QWidget *parent)
    : QMainWindow(parent)
{
    setMinimumSize(QSize(1280, 720));
    setStyleSheet("background-color:black;");;//设置样式
    _pOpenGLWidget = new MyOpenGLRender(this);
    setCentralWidget(_pOpenGLWidget);
//    _pOpenGLWidget->setGeometry(0, 0, width(), height());
//    _pOpenGLWidget->setAutoFillBackground(true);
    SingleInstance::GetInstanca().audioPlayer.init();
//    setCentralWidget(_pOpenGLWidget);
    setAutoFillBackground(true);
    setAcceptDrops(true);

}

void Screen::acceptProgress(double rotia)
{
    _decode.SetProgress(rotia);
}


void Screen::dragEnterEvent(QDragEnterEvent *event)
{
    auto file = event->mimeData()->urls().first();
    _filePath = file.toLocalFile().toUtf8();
    QFileInfo fileInfo(_filePath);

    auto suffix = fileInfo.suffix();
    if(suffix == "mp3" || suffix == "wav" || suffix == "flac" ||
            suffix == "mp4" || suffix == "avi" || suffix == "mkv")
    {
        event->acceptProposedAction();
    }else{
        event->ignore();
        _filePath.clear();
    }
}

void Screen::dropEvent(QDropEvent *event)
{
    Q_UNUSED(event)
    _filePath = QDir::toNativeSeparators(_filePath);
    _decode.SetFilePath(_filePath);
    _decode.SetCallBackRender(updateOpenGLData, reinterpret_cast<int64_t>(this));
    if(SingleInstance::GetInstanca().FirstLoadFile)
    {
        _decode.reset();
        //避免残留前一个视频的画面
        _pOpenGLWidget->reset();
//        delete  _pOpenGLWidget;
//        setCentralWidget(nullptr);

    }
    SingleInstance::GetInstanca().FirstLoadFile = true;
    SingleInstance::GetInstanca().LoadFile = true;
//    _pOpenGLWidget = new MyOpenGLRender(this);
    _pOpenGLWidget->setAutoFillBackground(true);
//    _pOpenGLWidget->resize(width(), height());
    setCentralWidget(_pOpenGLWidget);
    _decode.init();

    emit initDuration(_decode.GetDuration());
    emit reset();
}

void Screen::updateOpenGLData(std::unique_ptr<H264Frame> frame, int64_t mySelf)
{

    if(frame == nullptr || mySelf == 0)
    {
        qDebug() << "frame or mySelf is null";
        return;
    }
    auto me = reinterpret_cast<Screen*>(mySelf);
    if(me != nullptr)
    {
        me->_pOpenGLWidget->render(std::move(frame));
    }else{
        qDebug() << "mySelf is null";
    }
}


Screen::~Screen()
{

}

